package com.dunqi.gpm.cangxi.home.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.home.bean.Phone;
import com.dunqi.gpm.cangxi.home.bean.Project;
import com.dunqi.gpm.cangxi.home.eventbus.FinishAndRefresh;
import com.dunqi.gpm.cangxi.home.fragment.ProjectTimelineFragment;
import com.dunqi.gpm.cangxi.home.widget.ProjectPhoneAndDes;
import com.dunqi.gpm.cangxi.home.widget.ProjectTimeLineHeader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 用fragment  这个不做使用
 */
public class ProjectTimelineActivity extends BaseAct {
    private static final String PROJECT = "project";
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.projectPhoneAndDes)
    ProjectPhoneAndDes projectPhoneAndDes;
    @BindView(R.id.up_tv)
    TextView upTv;
    @BindView(R.id.project_time_line_header)
    ProjectTimeLineHeader projectTimeLineHeader;
    private Project project;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {

    }

    @Override
    protected void initUiAndListener() {
        EventBus.getDefault().register(this);
        Intent intent = getIntent();
        if (intent != null) {
            project = (Project) intent.getSerializableExtra(PROJECT);
        }
        titleBar.setTitle(project.getProjectName());
        projectPhoneAndDes.setData(project);
        ProjectTimelineFragment fragment = ProjectTimelineFragment.newInstance(project);
        getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
        projectTimeLineHeader.getGridView().setOnItemClickListener((parent, view, position, id)
                -> ProjectDetailActivity.start(getActivity(), project, position));
        upTv.setOnClickListener(v -> ProjectDetailActivity.start(getActivity(), project, 8));
        titleBar.setBackListener(v -> finish());
        getMore();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe
    public void onEventMainThread(FinishAndRefresh finishAndRefresh) {
        finish();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_timeline;
    }

    public static void start(Context context, Project project) {
        Intent intent = new Intent(context, ProjectTimelineActivity.class);
        intent.putExtra(PROJECT, project);
        context.startActivity(intent);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };

    @SuppressLint("CheckResult")
    void getMore() {
        ApiFactory.getInstance()
                .getOgazitionAll(project.getProjectId())
                .compose(RxSchedulers.io_main())
                .subscribe(projectPersonPage -> {
                    ArrayList<ProjectPerson> list = projectPersonPage.getList();
                    if (list != null && list.size() > 0) {
                        List<Phone> list_phone = new ArrayList<>();
                        for (ProjectPerson p : list) {
                            String type = "build_org".equals(p.getOrgTypeCode()) ? "施工单位领导" : "supervision_org".equals(p.getOrgTypeCode()) ? "监理单位" :
                                    "design_org".equals(p.getOrgTypeCode()) ? "设计单位" : p.getOrgName();
                            list_phone.add(new Phone(p.getUserAccount().getUserName(), p.getOrgLeaderPhone(), type));
                        }
                        projectPhoneAndDes.addData(list_phone);
                    }
                }, throwable -> {

                });
    }

    public class ProjectPerson {
        /**
         * orgId : null
         * userAccount : {"userId":null,"orgId":null,"accountType":null,"userName":"测试监理责任人","loginAccount":null,"password":null,"sex":null,"cellphone":null,"telno":null,"email":null,"registerTime":null,"lastLoginTime":null,"lastLoginIpAddr":null,"exField1":null,"exField2":null,"exField3":null,"status":null,"deviceId":null,"appToken":null,"tokenExpireDate":null,"systemAdmin":null,"createrId":null}
         * orgName : 测试监理单位
         * orgShortName : null
         * orgTypeCode : supervision_org
         * orgCode : null
         * parentOrgId : null
         * orgCaption : null
         * orgLogo : null
         * regionCode : null
         * orgAddr : null
         * orgLeaderId : null
         * orgLeaderPhone : 13509409458
         * pmLeadershipId : null
         * pmLeadershipPhone : null
         * contact : null
         * telphone : null
         * exField1 : null
         * exField2 : null
         * exField3 : null
         * createrId : null
         * createTime : null
         * enabled : null
         */

        private Object orgId;
        /**
         * userId : null
         * orgId : null
         * accountType : null
         * userName : 测试监理责任人
         * loginAccount : null
         * password : null
         * sex : null
         * cellphone : null
         * telno : null
         * email : null
         * registerTime : null
         * lastLoginTime : null
         * lastLoginIpAddr : null
         * exField1 : null
         * exField2 : null
         * exField3 : null
         * status : null
         * deviceId : null
         * appToken : null
         * tokenExpireDate : null
         * systemAdmin : null
         * createrId : null
         */

        private UserAccountBean userAccount;
        private String orgName;
        private Object orgShortName;
        private String orgTypeCode;
        private Object orgCode;
        private Object parentOrgId;
        private Object orgCaption;
        private Object orgLogo;
        private Object regionCode;
        private Object orgAddr;
        private Object orgLeaderId;
        private String orgLeaderPhone;
        private Object pmLeadershipId;
        private Object pmLeadershipPhone;
        private Object contact;
        private Object telphone;
        private Object exField1;
        private Object exField2;
        private Object exField3;
        private Object createrId;
        private Object createTime;
        private Object enabled;

        public String getOrgLeaderPhone() {
            return orgLeaderPhone;
        }

        public void setOrgLeaderPhone(String orgLeaderPhone) {
            this.orgLeaderPhone = orgLeaderPhone;
        }

        public Object getOrgId() {
            return orgId;
        }

        public void setOrgId(Object orgId) {
            this.orgId = orgId;
        }

        public UserAccountBean getUserAccount() {
            return userAccount;
        }

        public void setUserAccount(UserAccountBean userAccount) {
            this.userAccount = userAccount;
        }

        public String getOrgName() {
            return orgName;
        }

        public void setOrgName(String orgName) {
            this.orgName = orgName;
        }

        public Object getOrgShortName() {
            return orgShortName;
        }

        public void setOrgShortName(Object orgShortName) {
            this.orgShortName = orgShortName;
        }

        public String getOrgTypeCode() {
            return orgTypeCode;
        }

        public void setOrgTypeCode(String orgTypeCode) {
            this.orgTypeCode = orgTypeCode;
        }

        public Object getOrgCode() {
            return orgCode;
        }

        public void setOrgCode(Object orgCode) {
            this.orgCode = orgCode;
        }

        public Object getParentOrgId() {
            return parentOrgId;
        }

        public void setParentOrgId(Object parentOrgId) {
            this.parentOrgId = parentOrgId;
        }

        public Object getOrgCaption() {
            return orgCaption;
        }

        public void setOrgCaption(Object orgCaption) {
            this.orgCaption = orgCaption;
        }

        public Object getOrgLogo() {
            return orgLogo;
        }

        public void setOrgLogo(Object orgLogo) {
            this.orgLogo = orgLogo;
        }

        public Object getRegionCode() {
            return regionCode;
        }

        public void setRegionCode(Object regionCode) {
            this.regionCode = regionCode;
        }

        public Object getOrgAddr() {
            return orgAddr;
        }

        public void setOrgAddr(Object orgAddr) {
            this.orgAddr = orgAddr;
        }

        public Object getOrgLeaderId() {
            return orgLeaderId;
        }

        public void setOrgLeaderId(Object orgLeaderId) {
            this.orgLeaderId = orgLeaderId;
        }

        public class UserAccountBean {
            private Object userId;
            private Object orgId;
            private Object accountType;
            private String userName;
            private Object loginAccount;
            private Object password;
            private Object sex;
            private Object cellphone;
            private Object telno;
            private Object email;
            private Object registerTime;
            private Object lastLoginTime;
            private Object lastLoginIpAddr;
            private Object exField1;
            private Object exField2;
            private Object exField3;
            private Object status;
            private Object deviceId;
            private Object appToken;
            private Object tokenExpireDate;
            private Object systemAdmin;
            private Object createrId;

            public Object getUserId() {
                return userId;
            }

            public void setUserId(Object userId) {
                this.userId = userId;
            }

            public Object getOrgId() {
                return orgId;
            }

            public void setOrgId(Object orgId) {
                this.orgId = orgId;
            }

            public Object getAccountType() {
                return accountType;
            }

            public void setAccountType(Object accountType) {
                this.accountType = accountType;
            }

            public String getUserName() {
                return userName;
            }

            public void setUserName(String userName) {
                this.userName = userName;
            }

            public Object getLoginAccount() {
                return loginAccount;
            }

            public void setLoginAccount(Object loginAccount) {
                this.loginAccount = loginAccount;
            }

            public Object getPassword() {
                return password;
            }

            public void setPassword(Object password) {
                this.password = password;
            }

            public Object getSex() {
                return sex;
            }

            public void setSex(Object sex) {
                this.sex = sex;
            }

            public Object getCellphone() {
                return cellphone;
            }

            public void setCellphone(Object cellphone) {
                this.cellphone = cellphone;
            }

            public Object getTelno() {
                return telno;
            }

            public void setTelno(Object telno) {
                this.telno = telno;
            }

            public Object getEmail() {
                return email;
            }

            public void setEmail(Object email) {
                this.email = email;
            }

            public Object getRegisterTime() {
                return registerTime;
            }

            public void setRegisterTime(Object registerTime) {
                this.registerTime = registerTime;
            }

            public Object getLastLoginTime() {
                return lastLoginTime;
            }

            public void setLastLoginTime(Object lastLoginTime) {
                this.lastLoginTime = lastLoginTime;
            }

            public Object getLastLoginIpAddr() {
                return lastLoginIpAddr;
            }

            public void setLastLoginIpAddr(Object lastLoginIpAddr) {
                this.lastLoginIpAddr = lastLoginIpAddr;
            }

            public Object getExField1() {
                return exField1;
            }

            public void setExField1(Object exField1) {
                this.exField1 = exField1;
            }

            public Object getExField2() {
                return exField2;
            }

            public void setExField2(Object exField2) {
                this.exField2 = exField2;
            }

            public Object getExField3() {
                return exField3;
            }

            public void setExField3(Object exField3) {
                this.exField3 = exField3;
            }

            public Object getStatus() {
                return status;
            }

            public void setStatus(Object status) {
                this.status = status;
            }

            public Object getDeviceId() {
                return deviceId;
            }

            public void setDeviceId(Object deviceId) {
                this.deviceId = deviceId;
            }

            public Object getAppToken() {
                return appToken;
            }

            public void setAppToken(Object appToken) {
                this.appToken = appToken;
            }

            public Object getTokenExpireDate() {
                return tokenExpireDate;
            }

            public void setTokenExpireDate(Object tokenExpireDate) {
                this.tokenExpireDate = tokenExpireDate;
            }

            public Object getSystemAdmin() {
                return systemAdmin;
            }

            public void setSystemAdmin(Object systemAdmin) {
                this.systemAdmin = systemAdmin;
            }

            public Object getCreaterId() {
                return createrId;
            }

            public void setCreaterId(Object createrId) {
                this.createrId = createrId;
            }
        }
    }
}